package com.ajaxjs.user.controller;

import java.util.Map;

import javax.mvc.annotation.Controller;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

import com.ajaxjs.cms.service.CatalogService;
import com.ajaxjs.framework.service.ServiceException;
import com.ajaxjs.mvc.controller.CommonController;
import com.ajaxjs.user.User;
import com.ajaxjs.user.UserService;
import com.ajaxjs.util.collection.MapHelper;
import com.ajaxjs.util.ioc.Bean;
import com.ajaxjs.util.ioc.Resource;

/**
 * 用户注册控制器
 * 
 * @author Frank Cheung frank@ajaxjs.com
 */
@Controller
@Path("/user-open")
@Bean("RegisterController") 
public class RegisterController extends CommonController<User, Long> {
	public RegisterController() {
		setJSON_output(true);
	}
	
	@Resource("userService") // 指定 service id
	private UserService service;

	/**
	 * 注册
	 * @param map
	 * @param mv
	 * @return
	 */
	@POST
	public String create(Map<String, Object> _map) {
		Map<String, String> map = MapHelper.asString(_map);
		
		String userName = map.get("name").trim();	
		if(userName == null || userName.length() < 3) 
			return showJsonMsg(false, "用户名不能为空或者小于三个字符");
		
		String phoneNo = map.get("phone").trim();
		if(phoneNo == null || phoneNo.length() < 11) 
			return showJsonMsg(false, "手机号码不能为空或者小于13个字符");	
		
		String encryptedPassword = map.get("encryptedPassword");
		if(encryptedPassword == null) 
			return showJsonMsg(false, "注册密码不能为空");		
		
		User user = new User();
		user.setName(userName);
		user.setPhone(phoneNo);
		user.setPassword(encryptedPassword);
		
		try {
			getService().create(user);
		} catch (Exception e) {
			e.printStackTrace();
			//return showJsonMsg(false, e.toString());		
		}
		
		return String.format("json::{\"isOk\": true, \"cilentPassword\" : \"%s\"}", user.getPassword());
	}
	
	@POST
	@Path("login")
	public String login(){
		System.out.println("login");
		return null;
	}
}
